/// <reference path="../../sage/sage.util.js" />
/// <reference path="../painters/fourkill.painter.ZZ.singletons.js" />
/// <reference path="../fourkill.sprite.ZZ.singletons.js" />

var ServerObject = GameObject.extend({
    initialize: function initialize(sprite) {
        GameObject.initialize.call(this);

        this.BOUNDRY_SIZE = 1000;
        this.z = 0;
        this.speed = 0;
        this.turnSpeed = util.radians(5);
        this.boundaries = null;
        this.turnCounter = 0;
        this.turnDirection = 1;
        this.moveCounter = 0;
        this.sprite = sprite;
        this.serverLocation = null;
    },
    tick: function tick() {

        if (this.serverLocation != null) {
            //this.x = this.serverLocation.x;
            //this.y = this.serverLocation.y;
            var angleToPoint = util.angleToPoint(this, this.serverLocation);
            var d = util.findDistance2D(this, this.serverLocation);
            //this.bearing = util.moveTowardsAngle(this.angleFromBearing, angle, 5);

            if (d > 1) {
                this.speed = .25;
                if (d > 1000) this.speed = 500;
                else this.speed = d * .3;

                this.setBearing(angleToPoint);
                this.move(this.speed);
            }
            else {
                this.speed = 0;
                this.x = this.serverLocation.x;
                this.y = this.serverLocation.y;
            }

            /*if (this.geo != null) {
                        this.geo.updateLocation(context);
                        var c = context.camera,
                            l = this.geo.locationRelativeToCamera,
                            p = l.placement,
                            d = Math.abs(l.distance),
                            a = p.angleFromBearing, 
                            b1 = c.bearing,
                            b2 = b1;
                        if (d > 3 && !isNaN(a)) {
            
                            var absD = Math.abs(l.distance);
                            a += b1;
                            while (a > util._360) a -= util._360;
                            c.normalizeBearingToAngle(a, this._10);
                            b2 = c.bearing;
                            var diff = Math.abs(b2 - a);
            
                            if (diff < this._10 || (util._360 - diff) < this._10) {
                                if (absD > 0) {
                                    var speed = .5;
                                    if (absD > 1000) speed = 500;
                                    else if (absD > 500) speed = 100;
                                    else if (absD > 100) speed = 35;
                                    else if (absD > 80) speed = 25;
                                    else if (absD > 60) speed = 18;
                                    else if (absD > 20) speed = 12;
                                    else if (absD > 10) speed = 6;
                                    else if (absD > 5) speed = 2;
                                    else if (absD > 2) speed = 1;
            
                                    c.move(speed);
                                }
                            }
                        }
                    }*/
        }
        /*if (this.moveCounter > 0) {
            this.move(this.speed);
            this.moveCounter -= this.speed;
        }
        if (this.turnCounter > 0) {
            this.turn(this.turnSpeed * this.turnDirection);
            this.turnCounter -= this.turnSpeed;
        }

        if (this.moveCounter <= 0) {
            var r = util.random(100);
            if (r == 5) {
                this.speed = util.random() * util.random(1, 2) + 1;
                this.moveCounter = util.random() * util.random(10, 300);
                this.turnCounter = util.random() * util.random(45, 180);
                this.turnDirection = util.random(10) < 5 ? 1 : -1;
            }
            else if (r < 5) {
                this.speed = 0;
            }
        }*/
    },
    getPainter: function getPainter() { return Painters.AdvancedSpritePainter; }
});
